package com.issac.vhr.service;

import com.issac.vhr.entity.Hr;
import com.issac.vhr.entity.Menu;
import com.issac.vhr.mapper.MenuMapper;
import com.issac.vhr.mapper.Menu_roleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    Menu_roleMapper menu_roleMapper;
    public List<Menu> getMenuByHrId() {
        return menuMapper.getMenuByHrId(((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }

    //缓存注解
    //@Cacheable
    public List<Menu> getAllMenuWithRoles() {
        return menuMapper.getAllMenuWithRoles();
    }

    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    public List<Integer> getMidsByRid(Integer rid) {
        return menu_roleMapper.getMidsByRid(rid);
    }

    @Transactional
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        /*
        * 如果新修改的mids没有值，则只删除不插入
        *
        * */
        boolean deleteMenuByRid = menu_roleMapper.deleteMenuByRid(rid);
        if (mids==null || mids.length==0){
            return deleteMenuByRid;
        }else{
            Integer insertMenuByRid = menu_roleMapper.insertMenuByRid(rid, mids);
            return insertMenuByRid == mids.length;
        }
    }
}
